package cn.icare.cms.impl;

import cn.icare.cms.MoneyConfigService;
import cn.icare.cms.StringUtil;
import cn.icare.cms.dao.mapper.MoneyConfigMapper;
import cn.icare.cms.dao.po.MoneyConfig;
import cn.icare.cms.model.ro.AddMoneyConfigRO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MoneyConfigServiceImpl implements MoneyConfigService {

    @Resource
    private MoneyConfigMapper moneyConfigMapper;

    @Override
    public boolean saveOne(AddMoneyConfigRO ro, String actionUser) {
        boolean result = false;
        MoneyConfig model = this.getOne();
        if (model != null) {
            if (StringUtil.isNotEmpty(ro.getAlipayaccount())) {
                model.setAlipayaccount(ro.getAlipayaccount());
            }
            if (StringUtil.isNotEmpty(ro.getWxaccount())) {
                model.setWxaccount(ro.getWxaccount());
            }
            result = this.moneyConfigMapper.updateByPrimaryKeySelective(model) > 0;

        } else {
            model = new MoneyConfig();
            if (StringUtil.isNotEmpty(ro.getAlipayaccount())) {
                model.setAlipayaccount(ro.getAlipayaccount());
            }
            if (StringUtil.isNotEmpty(ro.getWxaccount())) {
                model.setWxaccount(ro.getWxaccount());
            }
            result = this.moneyConfigMapper.insertSelective(model) > 0;
        }
        return result;
    }

    @Override
    public MoneyConfig getOne() {
        List<MoneyConfig> dataList = this.moneyConfigMapper.selectAll();
        if (dataList != null && dataList.size() > 0) {
            return dataList.get(0);
        }
        return null;
    }
}
